package com.mes.biz.service.impl;

import com.mes.biz.domain.*;
import com.mes.biz.mapper.BizCustomerMapper;
import com.mes.biz.mapper.BizCustomerOpeningMapper;
import com.mes.biz.mapper.BizCustomerPayableDetailMapper;
import com.mes.biz.mapper.BizCustomerPayableMapper;
import com.mes.biz.service.BizCustomerOpeningService;
import com.mes.biz.service.BizCustomerPayableDetailService;
import com.mes.biz.service.BizCustomerPayableService;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Random;

/**
 * 客户应收期初Service业务层处理
 * @author zjw
 * @email zjw
 * @date 2023-06-06
 */
@Service
public class BizCustomerOpeningServiceImpl extends BaseServiceImpl<BizCustomerOpeningMapper, BizCustomerOpening> implements BizCustomerOpeningService {

    @Resource
    private BizCustomerOpeningMapper bizCustomerOpeningMapper;
    @Resource
    private BizCustomerPayableDetailMapper bizCustomerPayableDetailMapper;

    @Resource
    private BizCustomerPayableService bizCustomerPayableService;

    @Resource
    private BizCustomerPayableMapper bizCustomerPayableMapper;

    @Resource
    private BizCustomerPayableDetailService bizCustomerPayableDetailService;

    @Resource
    private BizCustomerMapper bizCustomerMapper;


    @Transactional(readOnly = false)
    @Override
    public int deleteBizCustomerOpeningByIds(String[] ids) {
        return mapper.deleteBizCustomerOpeningByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }


    @Override
    @Transactional
    public Boolean auditBizCustomerOpening(String id) {
        BizCustomerOpening bizCustomerOpening = super.get(id);
        if("0".equals(bizCustomerOpening.getStatus())) {
            Random random = new Random();
            //把随机生成的数字转成字符串
            String str = String.valueOf(random.nextInt(9));
            for (int i = 0; i < 5; i++) {
                str += random.nextInt(9);
            }
            BizCustomerPayable bizCustomerPayable = bizCustomerPayableService.getBizCustomerPayableByCustomerId(bizCustomerOpening.getCustomerId());
            if (null == bizCustomerPayable) {
                bizCustomerPayable = new BizCustomerPayable();
                bizCustomerPayable.setPayableCode("YSZK" + str);
                bizCustomerPayable.setTotalAmount(bizCustomerOpening.getTotalAmount());
                bizCustomerPayable.setHasAmount(bizCustomerOpening.getTotalAmount());
                bizCustomerPayable.setPayAmount(BigDecimal.ZERO);
                bizCustomerPayable.setCustomerId(bizCustomerOpening.getCustomerId());
                bizCustomerPayable.setTenantId(bizCustomerOpening.getTenantId());
                BizCustomer bizCustomer = bizCustomerMapper.get(bizCustomerOpening.getCustomerId());
                bizCustomerPayable.setCustomerName(bizCustomer.getCustomerName());
                bizCustomerPayableService.save(bizCustomerPayable);
            } else {
                bizCustomerPayable.setTotalAmount(bizCustomerPayable.getTotalAmount().add(bizCustomerOpening.getTotalAmount()));
                bizCustomerPayable.setHasAmount(bizCustomerPayable.getHasAmount().add(bizCustomerOpening.getTotalAmount()));
//                bizCustomerPayable.setPayAmount(bizCustomerPayable.getPayAmount().add(bizCustomerOpening.getCollectionlAmount()));
                bizCustomerPayableMapper.update(bizCustomerPayable);
            }
            BizCustomerPayableDetail payableDetail = new BizCustomerPayableDetail();
            payableDetail.setCustomerId(bizCustomerPayable.getCustomerId());
            payableDetail.setTotalAmount(bizCustomerOpening.getTotalAmount());
            payableDetail.setPayAmount(BigDecimal.ZERO);
            payableDetail.setOrderNumber("客户期初");
            payableDetail.setTenantId(bizCustomerPayable.getTenantId());
            bizCustomerPayableDetailService.save(payableDetail);
            bizCustomerOpening.setPayableDetailId(payableDetail.getId());
            bizCustomerOpening.setStatus("1");
            mapper.update(bizCustomerOpening);
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public Boolean doAuditBizCustomerOpening(String id) {
        BizCustomerOpening bizCustomerOpening = super.get(id);
        if("1".equals(bizCustomerOpening.getStatus())) {
            BizCustomerPayableDetail bizCustomerPayableDetail = bizCustomerPayableDetailService.get(bizCustomerOpening.getPayableDetailId());
            BizCustomerPayable bizCustomerPayable = bizCustomerPayableService.getBizCustomerPayableByCustomerId(bizCustomerOpening.getCustomerId());
            bizCustomerPayable.setTotalAmount(bizCustomerPayable.getTotalAmount().subtract(bizCustomerOpening.getTotalAmount()));
            bizCustomerPayable.setHasAmount(bizCustomerPayable.getHasAmount().subtract(bizCustomerOpening.getTotalAmount()));
//            bizCustomerPayable.setPayAmount(bizCustomerPayable.getPayAmount().subtract(bizCustomerOpening.getCollectionlAmount()));
            bizCustomerPayableMapper.update(bizCustomerPayable);
            bizCustomerPayableDetailService.remove(bizCustomerPayableDetail);
            bizCustomerOpening.setPayableDetailId("0");
            bizCustomerOpening.setStatus("0");
            mapper.update(bizCustomerOpening);
            return true;
        }
        return false;
    }
}
